module Nodes.NodeEditer exposing (..)
import Html exposing (Html,div,text,input,button,span)
import Html.Attributes exposing (style,value)
import Html.Events exposing (onClick,onInput)
import Http exposing (Error)
import Json.Encode as Encode
import Json.Decode as Decode
import Navigation
import Utils


type WrapMsg = WrapMsg Msg Id

type Msg
 = Confirm
 | UpdateResult (Result Error (Result String String))
 | InputTitle String
 | Delete
 | ConfirmDelete
 | CancelDelete
 | DelResult (Result Error (Result String String))

type Model = Model Id Title Log Delable 

initModel : Id -> Title -> Model
initModel id title = Model id title "" False

type alias Id = Int
type alias Title = String
type alias Log = String
type alias Delable = Bool

view : Model -> Html WrapMsg
view (Model id title log delable) =
 let 
  -- deleteView = button [onClick (WrapMsg Delete id)] [text "Delete"]
  deleteView = 
   case delable of
    False -> button [style [("background-color","red"),("color","white")],onClick (WrapMsg Delete id)] [text "Delete"]
    True -> 
     span [] 
      [ button [style [("background-color","red"),("color","white")],onClick (WrapMsg ConfirmDelete id)] [text "Confirm"]
      , button [style [("background-color","green"),("color","white")],onClick (WrapMsg CancelDelete id)] [text "Cancel" ]]
 in
  div [] 
   [ input [onInput (\str -> WrapMsg (InputTitle str) id),value title] []
   , button [onClick (WrapMsg Confirm id)] [text "ChangeTitle"]
   , deleteView
   , text ("Editer: " ++ log) ]



cmdUpdateNode : Model -> Cmd Msg
cmdUpdateNode (Model id title log _) =
 let 
  url = "/node/" ++ toString id
  body = Http.jsonBody (Encode.object [("title",Encode.string title)])
  decoder = Decode.oneOf [Decode.map Ok (Decode.field "ok" Decode.string),Decode.map Err (Decode.field "err" Decode.string)]
  req = Utils.put url body decoder
 in Http.send UpdateResult req


cmdDeleteNode : Model -> Cmd Msg
cmdDeleteNode (Model id title log _) =
 let 
  url = "/node/" ++ toString id
  decoder = Decode.oneOf [Decode.map Ok (Decode.field "ok" Decode.string),Decode.map Err (Decode.field "err" Decode.string)]
  req = Utils.del url decoder
 in Http.send DelResult req

update : WrapMsg -> Model -> (Model,Cmd WrapMsg)
update (WrapMsg msg theId) ((Model id title log delable) as model) = 
 if theId == id then
  case msg of
   Confirm -> (model,Cmd.map (\msg -> WrapMsg msg id) (cmdUpdateNode model))

   UpdateResult (Err err) -> (model,Cmd.none)
   UpdateResult (Ok res) -> 
    case res of
     Err err -> (Model id title (log ++ err) delable,Cmd.none)
     Ok _ -> (Model id title (log ++ " Delete Ok ") delable,Navigation.back 1)
   
   InputTitle str -> (Model id str log delable,Cmd.none)

   ConfirmDelete -> (model,Cmd.map (\msg -> WrapMsg msg id) (cmdDeleteNode model))

   Delete -> (Model id title log True,Cmd.none)

   CancelDelete -> (Model id title log False,Cmd.none)

   DelResult (Err err) -> (model,Cmd.none)
   DelResult (Ok res) ->
    case res of
     Err err -> (Model id title (log ++ err) delable,Cmd.none)
     Ok _ -> (Model id title (log ++ " Delete Ok ") delable,Navigation.back 1)

 else (model, Cmd.none)



find : Id -> List Model -> Maybe Model
find id models =
 models |> List.filter (\(Model modelId _ log _) -> modelId == id) |> List.head